package com.detroitchowfree;

import java.util.ArrayList;

import com.detroitchowfree.DetroitChowUtils;
import com.detroitchowfree.Main;
import com.detroitchowfree.R;
import com.detroitchowfree.StoreService;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ReviewActivity extends Activity implements IArticleUpdateObserver, OnClickListener, OnItemClickListener, IStoreServiceObserver{

	private StoreService storeService= null;
	private LinearLayout horizontalLinksLayout;
	private TextView txt;
	private Button _btnTag;
	private Context myContext;
	Intent intent;
	StoreVO selectedStore;
	String storeId=null;
	
	private ProgressDialog pd;
	private ArrayAdapter<Article> articleArrayAdapter=null;
	private ListView articlesListView=null;
	private TextView noArticlesText=null;
	private ImageButton btnLocation;
	private ImageButton btnRefresh;
	private ImageButton btnSearch;
	private ArrayList<Article> articles=null;
	private ImageView icon=null;
  private Intent myIntent=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	  // TODO Auto-generated method stub
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.article_main);
	  
	  AdRequest request = new AdRequest();
	  request.addTestDevice(AdRequest.TEST_EMULATOR);
    request.addTestDevice("014691441900D00F");
    AdView ad = (AdView) findViewById(R.id.ad);
    ad.loadAd(request);
    
	  LogUtil.log("ReviewActivity ONCreate", "OnCreateCalled");
	  btnRefresh = (ImageButton)  findViewById(R.id.btnRefresh);
    btnRefresh.setOnClickListener(this);
    
    btnLocation = (ImageButton)  findViewById(R.id.btnLocation);
    btnLocation.setOnClickListener(this);
   
    btnSearch = (ImageButton)  findViewById(R.id.btnSearch);
    btnSearch.setOnClickListener(this);
    
    icon = (ImageView) findViewById(R.id.icon);
    icon.setOnClickListener(this);
    
    if(storeService == null){
    	storeService = ((StoreService)getApplicationContext());
  	  
    }
    
	  
	  horizontalLinksLayout = (LinearLayout)this.findViewById(R.id.layoutTags);
	  myContext = this;
	  articlesListView = (ListView) this.findViewById(R.id.lvArticles);
	  
	  
    
	  
	}
	
	@Override
	protected void onResume() {
	  // TODO Auto-generated method stub
	  super.onResume();
	  storeId=null;
	  Bundle bundle = getIntent().getExtras();
  	storeId = 	bundle.getString(StoreService.STREAM_VALUE);
  	int _storeId=-1;
	  try{
	  	_storeId = Integer.parseInt(storeId);
	  }catch(Exception e){
	  	goHome();
	  }
	  
	  if(_storeId < 0 || !storeService.isStoresAlive()) {
	  	goHome();
	  }else {
	    selectedStore= storeService.getStore(_storeId);
	    if(selectedStore == null){
	    	goHome();
	    }else 
	    	processScreen();
	  }
  }
	
	private void processScreen() {
	  // TODO Auto-generated method stub
		horizontalLinksLayout.removeAllViewsInLayout();
  	ViewGroup.LayoutParams layoutParams;
  	
  	String txt = selectedStore.name;
	  _btnTag = (Button)getLayoutInflater().inflate(R.layout.linkitem, null);
	  _btnTag.setText(txt);
		layoutParams = new ViewGroup.LayoutParams(txt.length() * 25, 75);
	  _btnTag.setLayoutParams(layoutParams);
	  horizontalLinksLayout.addView(_btnTag);
		
		txt = selectedStore.address + " " + selectedStore.city;
		if(selectedStore.state != null)
			txt += " " + selectedStore.state;
		
		if(selectedStore.zip != null)
			txt += " " + selectedStore.zip;
			
		_btnTag = (Button)getLayoutInflater().inflate(R.layout.linkitem, null);
		_btnTag.setOnClickListener(navButtonClickListener);
		_btnTag.setText(txt);
		layoutParams = new ViewGroup.LayoutParams(txt.length() * 25, 75);
	  _btnTag.setLayoutParams(layoutParams);
	  horizontalLinksLayout.addView(_btnTag);
	  
		if(selectedStore.phone != null) {
			txt = DetroitChowUtils.formattedPhoneNumber(selectedStore.phone);
			_btnTag = (Button)getLayoutInflater().inflate(R.layout.linkitem, null);
			_btnTag.setOnClickListener(phoneButtonClickListener);
			_btnTag.setText(txt);
			layoutParams = new ViewGroup.LayoutParams(txt.length() * 25, 75);
		  _btnTag.setLayoutParams(layoutParams);
		  horizontalLinksLayout.addView(_btnTag);
		}
			
		  
		if(selectedStore.website != null && selectedStore.website.length() > 0) {
			
			txt =selectedStore.website.replaceAll("^http://","");
		  _btnTag = (Button)getLayoutInflater().inflate(R.layout.linkitem, null);
		  _btnTag.setText(txt);
			layoutParams = new ViewGroup.LayoutParams(txt.length() * 25, 75);
			_btnTag.setOnClickListener(webClickListener);
		  _btnTag.setLayoutParams(layoutParams);
		  horizontalLinksLayout.addView(_btnTag);
			
		}
		storeService.registerArticleUpdateObserver(this);
		storeService.getArticles(selectedStore.storeId);
	  
  }

	private void goHome () {
		myIntent = new Intent(this, Main.class);
		startActivityForResult(myIntent, 0);
	}
	
	public void onArticleUpdate(ArrayList<Article> _articles) {
		storeService.unRegisterArticleUpdateObserver(this);
	  // TODO Auto-generated method stub
		articles = _articles;
		if(articles != null) {
			articleArrayAdapter = new ArticleArrayAdapter(this, R.layout.reviewitem, -1, articles);
	    if(articleArrayAdapter != null){
	    	articlesListView.setAdapter(articleArrayAdapter);
	    	articlesListView.setTextFilterEnabled(true);
	    	articlesListView.setOnItemClickListener(this);
	    }
		}else{
			
			CharSequence text = "No Reviews or articles found for " + selectedStore.name;
			int duration = Toast.LENGTH_SHORT;
			Toast toast = Toast.makeText(this, text, duration);
			toast.show();
			
			
		}
			
		
    
  }
	
	//Create an anonymous implementation of OnClickListener
	private OnClickListener phoneButtonClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	String phone = "tel:" + selectedStore.phone;
	    	intent = new Intent(Intent.ACTION_CALL);
	    	intent.setData(Uri.parse(phone));
	    	myContext.startActivity(intent);
      }
	};
	
	
	
	//Create an anonymous implementation of OnClickListener
	private OnClickListener webClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	String url = selectedStore.website;
	    	intent = new Intent(Intent.ACTION_VIEW);
	    	intent.setData(Uri.parse(url));
	    	myContext.startActivity(intent);
	    }
	};	
	
	//Create an anonymous implementation of OnClickListener
	private OnClickListener navButtonClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	
	    	if(selectedStore.address != null) {
	    		String addressLine = selectedStore.address;
	    		if(selectedStore.city != null)
	    			addressLine += "+" + selectedStore.city;
	    		
	    		if(selectedStore.state != null)
	    			addressLine += "+" + selectedStore.state;
	    		
	    		if(selectedStore.zip != null)
	    			addressLine += "+" + selectedStore.zip;
	    		
	    		addressLine = addressLine.replaceAll(" ", "+");
	    		LogUtil.log("navButtonClickListener", addressLine);
	    		/*
	    		intent = new Intent(android.content.Intent.ACTION_VIEW, 
		    	Uri.parse("google.navigation:q=" + addressLine));
		    	*/
	    		Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
	    		Uri.parse("http://maps.google.com/maps?daddr=" + addressLine));
	    		myContext.startActivity(intent);
	    		
	    	}
	    }
	};

	public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent myIntent=null;
    if(v.equals(btnRefresh)) {
      LogUtil.log("onClick", "Click event is btnRefresh");
      storeService.registerUpdateStoresObserver(this);
      callProgressBar();
  		storeService.refreshStores();
    }if(v.equals(btnLocation)){
    	myIntent = new Intent(this, MapScreen.class);
    	myIntent.putExtra(MapScreen.INTENT_KEY, AppIntent.LOCATION);
    	startActivityForResult(myIntent, 0);
    }if(v.equals(btnSearch)){
    	myIntent = new Intent(this, SearchActivity.class);
    	startActivityForResult(myIntent, 0);
    }if(v.equals(icon)){
    	myIntent = new Intent(this, Main.class);
    	startActivityForResult(myIntent, 0);
    }
  }
    
    private void callProgressBar(){
  		pd = ProgressDialog.show(this, "Working..", "Refreshing Store List", true,
          false);
  	}
    
    public void onStoreManagerUpdate() {
  	  // TODO Auto-generated method stub
  		LogUtil.log("Main",  "onStoreManagerUpdate");
  		storeService.unRegisterUpdateStoresObserver(this);
  		if(pd != null)
        pd.dismiss();
    }

		public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {
	    // TODO Auto-generated method stub
			String url =  articles.get(index).articleUrl ;
			// get the intent
			intent = new Intent(Intent.ACTION_VIEW);
    	intent.setData(Uri.parse(url));
      myContext.startActivity(intent);
	    
    }
		
		
	
}
